package cn.jasonone.tio.tcp.model;

import cn.jasonone.tio.tcp.commons.Command;
import cn.jasonone.tio.tcp.config.constants.CharsetEnum;
import lombok.experimental.Accessors;
import org.tio.core.intf.Packet;

import java.nio.charset.Charset;

/**
 * 数据包定义
 *
 * @author xujie
 * @version 1.0
 * @date 2022/1/18 9:01
 */
@lombok.Data
@Accessors(chain = true)
public class Data extends Packet {
    private final Header header;

    private Object body;

    public Data() {
        header = new Header();
    }

    public Data(Header header, Object body) {
        this.header = header;
        this.body = body;
    }

    public Data(String bid, CharsetEnum charset, boolean encryption, Command command, String signature, Object body) {
        this.header = new Header(bid, charset, encryption, command, signature);
        this.body = body;
    }

    public Data(String bid, Charset charset, boolean encryption, Command command, String signature, Object body) {
        this.header = new Header(bid, CharsetEnum.valueOf(charset), encryption, command, signature);
        this.body = body;
    }

    public Data(boolean encryption, Command command, String signature, Object body) {
        this.header = new Header(encryption, command, signature);
        this.body = body;
    }
    public Data(Command command, Object body) {
        this.header = new Header(command);
        this.body = body;
    }

    public Data(Header header) {
        this.header = header;
    }

    /**
     * 业务ID(32 Byte)
     *
     * @return
     */
    public String getBid() {
        return header.getBid();
    }

    /**
     * 业务ID(32 Byte)
     *
     * @param id
     * @return
     */
    public Data setBId(String id) {
        this.header.setBid(id);
        return this;
    }

    /**
     * 字符编码(1 Byte)
     *
     * @return
     */
    public Charset getCharset() {
        return header.getCharset();
    }

    /**
     * 字符编码(1 Byte)
     *
     * @param charset
     * @return
     */
    public Data setCharset(Charset charset) {
        this.header.setCharset(charset);
        return this;
    }

    /**
     * 字符编码
     *
     * @param charsetCode
     * @return
     */
    public Data setCharsetCode(byte charsetCode) {
        this.header.setCharset(charsetCode);
        return this;
    }

    /**
     * 消息是否加密(1 Byte)
     *
     * @return
     */
    public boolean isEncryption() {
        return header.isEncryption();
    }

    /**
     * 消息是否加密(1 Byte)
     *
     * @param encryption
     * @return
     */
    public Data setEncryption(boolean encryption) {
        this.header.setEncryption(encryption);
        return this;
    }

    /**
     * 指令: 9 Byte
     * 指令类型: 1 Byte
     * 指令分组: 4 Byte
     * 指令编码: 4 Byte
     */
    public Command getCommand() {
        return header.getCommand();
    }

    /**
     * 指令: 9 Byte
     * 指令类型: 1 Byte
     * 指令分组: 4 Byte
     * 指令编码: 4 Byte
     */
    public Data setCommand(Command command) {
        this.header.setCommand(command);
        return this;
    }

    /**
     * 消息签名(最大32 Byte)
     *
     * @return
     */
    public String getSignature() {
        return this.header.getSignature();
    }

    /**
     * 消息签名(最大32 Byte)
     *
     * @param signature
     * @return
     */
    public Data setSignature(String signature) {
        this.header.setSignature(signature);
        return this;
    }

    /**
     * 消息体长度(8 Byte)
     *
     * @param bodyLength
     */
    public void setBodyLength(int bodyLength) {
        this.header.setBodyLength(bodyLength);
    }

    /**
     * 消息体长度(8 Byte)
     *
     * @return
     */
    public int getBodyLength() {
        return this.header.getBodyLength();
    }

}